﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MLY.LTR
{
    public partial class ApprovedMaterialsInProcess : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (SessParam.OrganizationId != 25 && SessParam.OrganizationId != 46)
                Response.Redirect("~/LTR/default.aspx");
            if (!IsPostBack)
            {
                ActualDataSource.SelectParameters.Add("CutOffDate", RetutnDateXWorkDaysAgo(ActualDropDownList.SelectedValue));
                SupplierApprovalDataSource.SelectParameters.Add("CutOffDate", RetutnDateXWorkDaysAgo(SupplierApprovalDropDownList.SelectedValue));
            }
        }
        private int TranslateWorkDaysToNormalDays(int Days)
        {
            int NormalDays = 0;
            DateTime Now = DateTime.Now;
            int WeekDay = (int)Now.DayOfWeek;
            if (WeekDay == 6)
            {
                Now = Now.AddDays(-1);
                WeekDay = 5;
            }
            if (WeekDay == 7)
            {
                Now = Now.AddDays(-2);
                WeekDay = 5;
            }

            while (Days > 5)
            {
                Days -= 5;
                NormalDays += 7;
            }
            NormalDays += Days;
            if (Days > WeekDay)
                NormalDays += 2;

            return NormalDays;
        }
        private int TranslateNormalDaysToWorkDays(int days)
        {
            int WorkDays = 0;
            DateTime Now = DateTime.Now;
            int WeekDay = (int)Now.DayOfWeek;
            if (WeekDay == 6)
            {
                WeekDay = 1;
                Now.AddDays(2);
                days -= 2;
            }
            if (WeekDay == 7)
            {
                WeekDay = 1;
                Now.AddDays(1);
                days -= 1;
            }

            while (days >= 7)
            {
                WorkDays += 5;
                days -= 7;
            }
            WorkDays += days;

            return WorkDays;
        }
        protected string CalculateTimeSince(object StartDate)
        {
            string ReturnValue;

            try
            {
                DateTime Now = DateTime.Now;
                DateTime SDate = DateTime.Parse(StartDate.ToString());
                TimeSpan Span = Now - SDate;
                ReturnValue = TranslateNormalDaysToWorkDays(Span.Days).ToString() + " days";
            }
            catch
            {
                ReturnValue = "unknown";
            }
            return ReturnValue;

        }

        protected string FormatDate(object Date)
        {
            string RetValue;

            try
            {
                RetValue = string.Format("{0:d}", DateTime.Parse(Date.ToString()));
            }
            catch
            {
                RetValue = ("");
            }
            return RetValue;


        }

        protected string CalculateOverdueTime(object StartDate, string ReportId)
        {
            string ReturnValue;
            int Report;
            int WorkDaysToAdd = 0;
            DateTime Now = DateTime.Now;
            int TimeSince;
            try
            {
                DateTime SDate = DateTime.Parse(StartDate.ToString());
                TimeSpan Span = Now - SDate;
                TimeSince = TranslateNormalDaysToWorkDays(Span.Days);
                Report = int.Parse(ReportId);
                switch (Report)
                {
                    case 1:
                        WorkDaysToAdd = int.Parse(ActualDropDownList.SelectedValue);
                        break;
                    case 2:
                        WorkDaysToAdd = int.Parse(SupplierApprovalDropDownList.SelectedValue);
                        break;                    
                }
                ReturnValue = (TimeSince - WorkDaysToAdd).ToString() + " days";
            }
            catch
            {
                ReturnValue = "unknown";
            }

            return ReturnValue;
        }
        private string RetutnDateXWorkDaysAgo(string Days)
        {
            // calcualtares the date that was X work days ago (holidays not implemented)
            string ReturnValue;
            int TotalDaysCount = 0;
            int DaysLeftToCount;
            DateTime Date = DateTime.Now;

            try
            {
                DaysLeftToCount = int.Parse(Days);
                TotalDaysCount = TranslateWorkDaysToNormalDays(DaysLeftToCount);
                Date = Date.AddDays(-TotalDaysCount);
                ReturnValue = ReturnSQLFormattedString(Date);
            }
            catch
            {
                ReturnValue = "unknown ";
            }

            return ReturnValue;
        }

        private string ReturnSQLFormattedString(DateTime Date)
        {
            return (Date.Year.ToString() + "-" + Date.Month.ToString() + "-" + Date.Day.ToString() + " 00:00:00");
        }

        protected void SupplierApprovalRefresh(object sender, EventArgs e)
        {
            SupplierApprovalDataSource.SelectParameters.Clear();
            SupplierApprovalDataSource.SelectParameters.Add("CutOffDate", RetutnDateXWorkDaysAgo(SupplierApprovalDropDownList.SelectedValue));
            SupplierApprovalDataSource.SelectParameters.Add("YesNo", SupplierApprovalYeNoDDL.SelectedValue);
            SupplierApprovalGrid.DataBind();

        }

        protected void ActualRefresh(object sender, EventArgs e)
        {
            ActualDataSource.SelectParameters.Clear();
            ActualDataSource.SelectParameters.Add("CutOffDate", RetutnDateXWorkDaysAgo(ActualDropDownList.SelectedValue));
            ActualDataSource.SelectParameters.Add("YesNo", ActualYesNoDDL.SelectedValue);
            ActualGridview.DataBind();
        }

        protected void ExportToExcel(object Sender, EventArgs e)
        {
            if (((ImageButton)Sender).ID == "ExportToExcel1")
            {
                MLY.Bll.ExcelExport.PrepareCsvFileFromGridview(ActualGridview, "WithActual_NoSupplierApproval");
            }
            else if (((ImageButton)Sender).ID == "ExportToExcel2")
            {
                MLY.Bll.ExcelExport.PrepareCsvFileFromGridview(SupplierApprovalGrid, "WithSupplierApproval");
            }

            
        }

    }
}
